from utils.common import *
from datetime import datetime, timedelta


class LoanUnfakeV2():

    def __init__(self, loan_un_base_v2):
        self.order_apply_time = loan_un_base_v2.order_apply_time
        self.order_apply_time_datetime = loan_un_base_v2.order_apply_time_datetime
        self.order_apply_date = loan_un_base_v2.order_apply_date
        self.installments_data_df = loan_un_base_v2.installments_data_df

    def gen_features(self):
        feature_dict = {}

        self.__gen_fake_feature(self.installments_data_df,feature_dict)

        for key, value in feature_dict.items():
            feature_dict[key] = float(value) if not np.isnan(value) else -999.0

        return feature_dict

    def __gen_fake_feature(self, installments_data_df,feature_dict):

        # 还款订单｜结清订单
        repay_data_df_sort = installments_data_df[(installments_data_df['status'] == 2)].sort_values(by='update_time')
        if len(repay_data_df_sort) >= 1:
            # 第一次还款订单
            first_repay_data_df = repay_data_df_sort.iloc[0]
            fake_first_repay_early_day = (datetime.strptime(first_repay_data_df['repayment_date'], "%Y-%m-%d") - datetime.strptime(first_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S")).total_seconds() / (60*60*24)
            fake_first_repay_onloan_day = (datetime.strptime(first_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(first_repay_data_df['create_time'], "%Y-%m-%d %H:%M:%S")).total_seconds() / (60*60*24)
            fake_first_repay_loan_amount = first_repay_data_df['principal']
            fake_first_repay_loan_time_diff = (datetime.strptime(first_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(first_repay_data_df['create_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
            fake_apply_first_repay_time_diff = (self.order_apply_time_datetime - datetime.strptime(first_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
            fake_apply_first_loan_time_diff = (self.order_apply_time_datetime - datetime.strptime(first_repay_data_df['create_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
            # 上一次还款订单
            last_repay_data_df = repay_data_df_sort.iloc[-1]
            fake_apply_last_repay_time_diff = (self.order_apply_time_datetime - datetime.strptime(last_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
            fake_apply_last_loan_time_diff = (self.order_apply_time_datetime - datetime.strptime(last_repay_data_df['create_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
        else:
            fake_first_repay_early_day = float('nan')
            fake_first_repay_onloan_day = float('nan')
            fake_first_repay_loan_amount = float('nan')
            fake_first_repay_loan_time_diff = float('nan')
            fake_apply_first_repay_time_diff = float('nan')
            fake_apply_first_loan_time_diff = float('nan')
            fake_apply_last_repay_time_diff = float('nan')
            fake_apply_last_loan_time_diff = float('nan')
        if len(repay_data_df_sort) >= 2:
            # 第二次还款订单
            second_repay_data_df = repay_data_df_sort.iloc[1]
            fake_second_repay_early_day = (datetime.strptime(second_repay_data_df['repayment_date'], "%Y-%m-%d") - datetime.strptime(second_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S")).total_seconds() / (60*60*24)
            fake_second_repay_onloan_day = (datetime.strptime(second_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(second_repay_data_df['create_time'], "%Y-%m-%d %H:%M:%S")).total_seconds() / (60*60*24)
            fake_second_repay_loan_amount = second_repay_data_df['principal']
            fake_second_first_loan_amount_diff = fake_second_repay_loan_amount - fake_first_repay_loan_amount
            fake_second_first_loan_amount_ratio = divide(fake_second_repay_loan_amount,fake_first_repay_loan_amount)
            fake_top2_repay_early_day = fake_first_repay_early_day+fake_second_repay_early_day
            fake_top2_repay_onloan_day = fake_first_repay_onloan_day+fake_second_repay_onloan_day
            fake_second_first_loan_amount_diff_ratio = divide(fake_second_first_loan_amount_diff,fake_top2_repay_onloan_day)
            fake_second_repay_loan_time_diff = (datetime.strptime(second_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(second_repay_data_df['create_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
            fake_second_apply_first_repay_time_diff = (datetime.strptime(second_repay_data_df['apply_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(first_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
            fake_second_loan_first_repay_time_diff = (datetime.strptime(second_repay_data_df['create_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(first_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
        else:
            fake_second_repay_early_day = float('nan')
            fake_second_repay_onloan_day = float('nan')
            fake_second_repay_loan_amount = float('nan')
            fake_second_first_loan_amount_diff = float('nan')
            fake_second_first_loan_amount_ratio = float('nan')
            fake_top2_repay_early_day = float('nan')
            fake_top2_repay_onloan_day = float('nan')
            fake_second_first_loan_amount_diff_ratio = float('nan')
            fake_second_repay_loan_time_diff = float('nan')
            fake_second_apply_first_repay_time_diff = float('nan')
            fake_second_loan_first_repay_time_diff = float('nan')
        if len(repay_data_df_sort) >= 3:
            # 第三次还款订单
            third_repay_data_df = repay_data_df_sort.iloc[2]
            fake_third_repay_early_day = (datetime.strptime(third_repay_data_df['repayment_date'], "%Y-%m-%d") - datetime.strptime(third_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S")).total_seconds() / (60*60*24)
            fake_third_repay_onloan_day = (datetime.strptime(third_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(third_repay_data_df['create_time'], "%Y-%m-%d %H:%M:%S")).total_seconds() / (60*60*24)
            fake_third_repay_loan_amount = third_repay_data_df['principal']
            fake_third_first_loan_amount_diff = fake_third_repay_loan_amount - fake_first_repay_loan_amount
            fake_third_first_loan_amount_ratio = divide(fake_third_repay_loan_amount,fake_first_repay_loan_amount)
            fake_third_second_loan_amount_diff = fake_third_repay_loan_amount - fake_second_repay_loan_amount
            fake_third_second_loan_amount_ratio = divide(fake_third_repay_loan_amount,fake_second_repay_loan_amount)
            fake_top3_repay_early_day = fake_first_repay_early_day+fake_second_repay_early_day+fake_third_repay_early_day
            fake_top3_repay_onloan_day = fake_first_repay_onloan_day+fake_second_repay_onloan_day+fake_third_repay_onloan_day
            fake_third_first_loan_amount_diff_ratio = divide(fake_third_first_loan_amount_diff, fake_top3_repay_onloan_day)
            fake_third_second_loan_amount_diff_ratio = divide(fake_third_second_loan_amount_diff, fake_top3_repay_onloan_day)
            fake_third_repay_loan_time_diff = (datetime.strptime(third_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(third_repay_data_df['create_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
            fake_third_apply_first_repay_time_diff = (datetime.strptime(third_repay_data_df['apply_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(first_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
            fake_third_loan_first_repay_time_diff = (datetime.strptime(third_repay_data_df['create_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(first_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
            fake_third_apply_second_repay_time_diff = (datetime.strptime(third_repay_data_df['apply_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(second_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
            fake_third_loan_second_repay_time_diff = (datetime.strptime(third_repay_data_df['create_time'], "%Y-%m-%d %H:%M:%S") - datetime.strptime(second_repay_data_df['update_time'], "%Y-%m-%d %H:%M:%S")).total_seconds()
        else:
            fake_third_repay_early_day = float('nan')
            fake_third_repay_onloan_day = float('nan')
            fake_third_repay_loan_amount = float('nan')
            fake_third_first_loan_amount_diff = float('nan')
            fake_third_first_loan_amount_ratio = float('nan')
            fake_third_second_loan_amount_diff = float('nan')
            fake_third_second_loan_amount_ratio = float('nan')
            fake_top3_repay_early_day = float('nan')
            fake_top3_repay_onloan_day = float('nan')
            fake_third_first_loan_amount_diff_ratio = float('nan')
            fake_third_second_loan_amount_diff_ratio = float('nan')
            fake_third_repay_loan_time_diff = float('nan')
            fake_third_apply_first_repay_time_diff = float('nan')
            fake_third_loan_first_repay_time_diff = float('nan')
            fake_third_apply_second_repay_time_diff = float('nan')
            fake_third_loan_second_repay_time_diff = float('nan')

        feature_dict['fake_first_repay_early_day']=fake_first_repay_early_day
        feature_dict['fake_first_repay_onloan_day']=fake_first_repay_onloan_day
        feature_dict['fake_first_repay_loan_amount']=fake_first_repay_loan_amount
        feature_dict['fake_first_repay_loan_time_diff']=fake_first_repay_loan_time_diff
        feature_dict['fake_apply_first_repay_time_diff']=fake_apply_first_repay_time_diff
        feature_dict['fake_apply_first_loan_time_diff']=fake_apply_first_loan_time_diff
        feature_dict['fake_apply_last_repay_time_diff']=fake_apply_last_repay_time_diff
        feature_dict['fake_apply_last_loan_time_diff']=fake_apply_last_loan_time_diff
        feature_dict['fake_second_repay_early_day']=fake_second_repay_early_day
        feature_dict['fake_second_repay_onloan_day']=fake_second_repay_onloan_day
        feature_dict['fake_second_repay_loan_amount']=fake_second_repay_loan_amount
        feature_dict['fake_second_first_loan_amount_diff']=fake_second_first_loan_amount_diff
        feature_dict['fake_second_first_loan_amount_ratio']=fake_second_first_loan_amount_ratio
        feature_dict['fake_top2_repay_early_day']=fake_top2_repay_early_day
        feature_dict['fake_top2_repay_onloan_day']=fake_top2_repay_onloan_day
        feature_dict['fake_second_first_loan_amount_diff_ratio']=fake_second_first_loan_amount_diff_ratio
        feature_dict['fake_second_repay_loan_time_diff']=fake_second_repay_loan_time_diff
        feature_dict['fake_second_apply_first_repay_time_diff']=fake_second_apply_first_repay_time_diff
        feature_dict['fake_second_loan_first_repay_time_diff']=fake_second_loan_first_repay_time_diff
        feature_dict['fake_third_repay_early_day']=fake_third_repay_early_day
        feature_dict['fake_third_repay_onloan_day']=fake_third_repay_onloan_day
        feature_dict['fake_third_repay_loan_amount']=fake_third_repay_loan_amount
        feature_dict['fake_third_first_loan_amount_diff']=fake_third_first_loan_amount_diff
        feature_dict['fake_third_first_loan_amount_ratio']=fake_third_first_loan_amount_ratio
        feature_dict['fake_third_second_loan_amount_diff']=fake_third_second_loan_amount_diff
        feature_dict['fake_third_second_loan_amount_ratio']=fake_third_second_loan_amount_ratio
        feature_dict['fake_top3_repay_early_day']=fake_top3_repay_early_day
        feature_dict['fake_top3_repay_onloan_day']=fake_top3_repay_onloan_day
        feature_dict['fake_third_first_loan_amount_diff_ratio']=fake_third_first_loan_amount_diff_ratio
        feature_dict['fake_third_second_loan_amount_diff_ratio']=fake_third_second_loan_amount_diff_ratio
        feature_dict['fake_third_repay_loan_time_diff']=fake_third_repay_loan_time_diff
        feature_dict['fake_third_apply_first_repay_time_diff']=fake_third_apply_first_repay_time_diff
        feature_dict['fake_third_loan_first_repay_time_diff']=fake_third_loan_first_repay_time_diff
        feature_dict['fake_third_apply_second_repay_time_diff']=fake_third_apply_second_repay_time_diff
        feature_dict['fake_third_loan_second_repay_time_diff']=fake_third_loan_second_repay_time_diff
